package com.token.sysflow.impl;

import com.alibaba.fastjson.JSONObject;
import com.token.base.JsonGenerator;
import com.token.base.Pl;
import com.token.sql.IntegrateSql;
import com.token.sysflow.core.LoadFlowStep;
import com.token.sysflow.enumerate.ActionTypeEnum;
import com.token.sysflow.enumerate.FlowStatusEnum;
import com.token.sysflow.hander.ApproveProcess;
import com.token.sysflow.hander.Flow;
import com.token.sysflow.hander.Step;
import lombok.Getter;
import lombok.Setter;

/**
 * 取回流程
 *
 * @author chenjianxiong
 */

public class LoadFlowStepImpl extends AbstractFlowHandlerImpl {
    /**
     * 起草进入
     */
    public final static int TYPE_FIRST = 1;

    /**
     * 审批进入
     * 要求传入：flowId,stepId。
     */
    public final static int TYPE_APPROVAL = 2;

    /**
     * 类型：起草进入，审批进入
     */
    @Getter
    @Setter
    private int loadType;
    /**
     * 应用KeyId
     */
    @Setter
    @Getter
    private String appKeyId;

    /**
     * 申请人
     */
    @Setter
    @Getter
    private String applyer;
    /**
     * 初始化参数
     *
     * @param iSql 类型
     * @throws Exception 抛出错误
     */
    public void initParameter(IntegrateSql iSql) throws Exception {
        if(loadType == LoadFlowStepImpl.TYPE_FIRST) {
            flowCore = new LoadFlowStep(iSql);
        }else if(loadType == LoadFlowStepImpl.TYPE_APPROVAL){
            flowCore = new LoadFlowStep(iSql,keyUnid);
        }
    }

    /**
     * 提交操作类型
     *
     * @return int
     */
    public ActionTypeEnum getActionType() {
        return ActionTypeEnum.LOADFLOW;
    }

    /**
     *
     * @throws Exception
     */
    public void handleReturnResult() throws Exception {
        Flow flow = flowCore.getFromFlow();
        Step step = flowCore.getFromStep();
        this.setReturnJson("flowId", flow.getFlowId());
        this.setReturnJson("flowName", flow.getFlowName());
        this.setReturnJson("stepId", step.getStepId());
        this.setReturnJson("stepName", step.getStepName());
        this.setReturnJson("curTime", Pl.getCurDataTime());

        this.setReturnJson("ApplyerTitle", flowCore.getApplyerPerson().getPersonName());
        this.setReturnJson("CreaterTitle", flowCore.getCreatePerson().getPersonName());

        ApproveProcess process = flowCore.getApproveProcess();
        if(process != null){
            this.setReturnJson("flowStatus",process.getFlowStatus());
        }else{
            this.setReturnJson("flowStatus", FlowStatusEnum.START.getId());
        }

        //输出控制。
        JSONObject control = JsonGenerator.toJson(step.getStepControl());
        control.remove("FlowId");
        control.remove("StepId");
        this.setReturnJson("control", control);

        //输出开关
        JSONObject switchFunc = JsonGenerator.toJson(step.getStepSwitchFunc());
        switchFunc.remove("FlowId");
        switchFunc.remove("StepId");
        this.setReturnJson("switchFunc", switchFunc);
    }
}